$(document).ready(function() {
    // 从localStorage获取搜索历史
    let searchHistory = JSON.parse(localStorage.getItem('searchHistory')) || [];
    
    // 添加搜索分组显示
    function renderSearchResults(searchTerm, matchedHistory) {
        let html = '';
        
        if (searchTerm) {
            // 显示当前搜索
            html += `
                <div class="search-group-title">搜索建议</div>
                <div class="search-item">
                    <i class="bi bi-search"></i>
                    <span>搜索 "${searchTerm}"</span>
                </div>
            `;
            
            // 显示匹配的历史记录
            if (matchedHistory.length > 0) {
                html += `
                    <div class="search-group-title">历史记录</div>
                    ${matchedHistory.map(item => `
                        <div class="search-item">
                            <i class="bi bi-clock-history"></i>
                            <span>${item}</span>
                            <i class="bi bi-x-circle delete-search" data-term="${item}"></i>
                        </div>
                    `).join('')}
                `;
            }
        } else if (searchHistory.length > 0) {
            // 显示最近搜索记录
            html += `
                <div class="search-group-title">最近搜索</div>
                ${searchHistory.slice(0, 10).map(item => `
                    <div class="search-item">
                        <i class="bi bi-clock-history"></i>
                        <span>${item}</span>
                        <i class="bi bi-x-circle delete-search" data-term="${item}"></i>
                    </div>
                `).join('')}
            `;
        }
        
        return html;
    }
    
    // 修改搜索框输入事件处理
    $('#searchInput').on('input', function() {
        const searchTerm = $(this).val().trim();
        const $searchResults = $('#searchResults');
        
        const matchedHistory = searchHistory.filter(item => 
            item.toLowerCase().includes(searchTerm.toLowerCase())
        );
        
        const html = renderSearchResults(searchTerm, matchedHistory);
        if (html) {
            $searchResults.html(html).show();
        } else {
            $searchResults.hide();
        }
    });
    
    // 提交搜索表单
    $('.search-form').on('submit', function(e) {
        e.preventDefault();
        const searchTerm = $('#searchInput').val().trim();
        
        if (searchTerm) {
            // 添加到搜索历史
            if (!searchHistory.includes(searchTerm)) {
                searchHistory.unshift(searchTerm);
                // 保持最多10条记录
                if (searchHistory.length > 10) {
                    searchHistory.pop();
                }
                // 保存到localStorage
                localStorage.setItem('searchHistory', JSON.stringify(searchHistory));
            }
            
            // 跳转到搜索结果页面
            window.location.href = `search.html?q=${encodeURIComponent(searchTerm)}`;
        }
    });
    
    // 点击搜索建议项
    $(document).on('click', '.search-item span', function() {
        const term = $(this).text();
        $('#searchInput').val(term);
        $('.search-form').submit();
    });
    
    // 删除搜索历史项
    $(document).on('click', '.delete-search', function(e) {
        e.stopPropagation();
        const term = $(this).data('term');
        searchHistory = searchHistory.filter(item => item !== term);
        localStorage.setItem('searchHistory', JSON.stringify(searchHistory));
        $(this).parent().remove();
        
        if (searchHistory.length === 0) {
            $('#searchResults').hide();
        }
    });
    
    // 点击页面其他地方时隐藏搜索结果
    $(document).on('click', function(e) {
        if (!$(e.target).closest('.search-form').length) {
            $('#searchResults').hide();
        }
    });
    
    // 聚焦搜索框时显示搜索历史
    $('#searchInput').on('focus', function() {
        if (searchHistory.length > 0 && !$(this).val()) {
            $('#searchResults').show();
        }
    });
    
    // 添加清空所有历史记录功能
    function addClearAllButton() {
        if (searchHistory.length > 0) {
            return `
                <div class="search-item clear-all">
                    <i class="bi bi-trash"></i>
                    <span>清空搜索历史</span>
                </div>
            `;
        }
        return '';
    }
    
    // 清空所有历史记录
    $(document).on('click', '.clear-all', function() {
        searchHistory = [];
        localStorage.removeItem('searchHistory');
        $('#searchResults').hide();
    });
}); 